package com.aaa.web.controller; /**
 * @ProjectName Java_web_demo_20230304
 * @Author 李江波
 * @Date 2023/03/04 上午 09:25
 * @Version 1.0.0
 * @Description
 */

import com.aaa.web.entity.Dept;
import com.aaa.web.service.dept.DeptServiceImpl;
import com.aaa.web.service.dept.Impl.DeptService;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "DeptServlet", value = "/DeptServlet/*")
public class DeptServlet extends HttpServlet {

    private DeptService deptService = new DeptServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取url或者uri
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        StringBuffer requestURL = req.getRequestURL();
        //url=localhost:8080/EmpServlet?toUpdate 通过url定位到资源 （河南省郑州市高新区科学大道与长春路交叉口向西200米郑大南门）
        System.out.println("请求的URL = " + requestURL);
        String requestURI = req.getRequestURI();
        //uri = EmpServlet/ToUpdate 只是资源的名称 无法定位到具体资源  （郑州大学）
        System.out.println("请求的URI = " + requestURI);
        //截取字符串。获取执行请求的最后部门字符串  requestMethodName = query add toUpdate update delete其中一个
        String requestMethodName = requestURI.substring(requestURI.lastIndexOf("/") + 1);
        //定义返回字符串
        String jsonData =null;
        //执行各种方法
        switch(requestMethodName){
            case "queryPage":
                jsonData =  this.queryPage(req,resp);
                break;
            case "queryById":
                jsonData =  this.queryById(req,resp);
                break;
            case "update":
                jsonData =  this.update(req,resp);
                break;
            case "add":
                jsonData =  this.add(req,resp);
                break;
            case "delete":
                jsonData =  this.delete(req,resp);
                break;
            default:
                break;
        }
        if (jsonData!=null){
            //定义返回类型是json 并且支持中文编码
            resp.setContentType("application/json;charset=utf-8");
            PrintWriter out = resp.getWriter();
            out.println(jsonData);
        }
    }

    private String queryById(HttpServletRequest req, HttpServletResponse resp) {
        //接受参数  pageNo   deptName  loc
        String deptNo = req.getParameter("deptNo");
        System.out.println("deptNo = " + deptNo);
        Dept dept = deptService.queryById(Integer.valueOf(deptNo));
        return JSONObject.toJSONString(dept);
    }

    private String queryPage(HttpServletRequest req, HttpServletResponse resp) {
        //接受参数  pageNo   deptName  loc
        String pageNo = req.getParameter("pageNo");
        String deptName = req.getParameter("deptName");
        String loc = req.getParameter("loc");

        //封装查询对象
        Dept queryDept = new Dept();
        queryDept.setDeptName(deptName);
        queryDept.setLoc(loc);

        //定义数字类型 页面
        int pageNoInt = 0;
        //如果第几页没有传递  给默认值
        if(pageNo==null){
            pageNoInt = 1;
        }else {
            //赋值
            pageNoInt = Integer.valueOf(pageNo);
        }
        //判断是否小于1
        if(pageNoInt<1){
            pageNoInt = 1;
        }
        //定义每页显示数量
        int pageSize = 5;
        //查询分页总数量
        int total = deptService.queryTotal(queryDept);
        if (total==0){
            req.setAttribute("error","没有查询到结果");
        }
        // 计算最大页
        int maxPage = total%pageSize==0?total/pageSize:(total/pageSize+1);

        //判断是否最大页，如果大于最大页，等于最大页
        if(pageNoInt>maxPage){
            pageNoInt = maxPage;
        }
        //执行分页查询数据
        List<Dept> deptList = deptService.queryByPage(pageNoInt, pageSize, queryDept);
        System.out.println("deptList = " + deptList);

        //定义一个返回的map对象
        Map resultMap = new HashMap<>();
        //分页查询数据结果
        resultMap.put("deptList",deptList);
        //总条数
        resultMap.put("total",total);
        //总页数
        resultMap.put("maxPage",maxPage);
        //当前页
        resultMap.put("pageNo",pageNoInt);

       return JSONObject.toJSONString(resultMap);

    }

    private String add(HttpServletRequest req, HttpServletResponse resp) {
        String deptName = req.getParameter("deptName");
        String loc = req.getParameter("loc");
        Dept dept = new Dept(deptName,loc);
        return deptService.add(dept)+"";
    }
    private String update(HttpServletRequest req, HttpServletResponse resp) {
        String deptNo = req.getParameter("deptNo");
        String deptName = req.getParameter("deptName");
        String loc = req.getParameter("loc");
        //实例化部门对象
        Dept dept = new Dept(Integer.valueOf(deptNo),deptName,loc);
        return deptService.update(dept)+"";
    }

    private String delete(HttpServletRequest req, HttpServletResponse resp) {
        //接受参数
        String deptNo = req.getParameter("deptNo");
        //调用删除方法
        return deptService.deleteById(Integer.valueOf(deptNo))+"";
    }
}
